package com.blame.springblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blame.springblog.common.exception.BlogException;
import com.blame.springblog.common.utils.BeanTranUtils;
import com.blame.springblog.common.utils.JwtUtils;
import com.blame.springblog.common.utils.SecurityUtils;
import com.blame.springblog.constant.Constants;
import com.blame.springblog.mapper.UserInfoMapper;
import com.blame.springblog.pojo.dataobject.BlogInfo;
import com.blame.springblog.pojo.dataobject.UserInfo;
import com.blame.springblog.pojo.request.UserLoginRequest;
import com.blame.springblog.pojo.response.UserInfoResponse;
import com.blame.springblog.pojo.response.UserLoginResponse;
import com.blame.springblog.service.BlogInfoService;
import com.blame.springblog.service.UserInfoService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Resource(name = "blogInfoServiceImpl")
    private BlogInfoService blogInfoService;


    @Override
    public UserLoginResponse userLogin(UserLoginRequest userLoginRequest) {
        if(userLoginRequest==null){
            log.error("请求为空");
            throw new BlogException("请求为空");
        }
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();

        queryWrapper.lambda()
                .eq(UserInfo::getUserName,userLoginRequest.getUserName())
                .eq(UserInfo::getDeleteFlag, Constants.FLAG_NORMAL);
        UserInfo userInfo=userInfoMapper.selectOne(queryWrapper);
        if(userInfo==null){
            throw new BlogException("用户不存在");
        }
        if(!SecurityUtils.verify(userLoginRequest.getPassword(),userInfo.getPassword())){
            throw new BlogException("请输入正确的密码");
        }
        Map<String, Object> map=new HashMap<>();
        map.put("userId",userInfo.getId());
        map.put("userName",userInfo.getUserName());

        String token= JwtUtils.genJwt(map);

        return new UserLoginResponse(userInfo.getId(),token);
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getDeleteFlag,Constants.FLAG_NORMAL)
                .eq(UserInfo::getId,userId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        return BeanTranUtils.tran(userInfo);
    }

    @Override
    public UserInfoResponse getAuthorId(Integer blogId) {
        //根据博客Id获得博客信息
        BlogInfo blogInfo = blogInfoService.getBlogInfo(blogId);
        if(blogInfo==null || blogInfo.getUserId()<=0){
            throw new BlogException("获取博客信息失败");
        }
        //根据博客信息获得作者信息
        return getUserInfo(blogInfo.getUserId());
    }
}
